package cc.comac.mom.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairRepositorySparePartsService;
import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.bean.EquipRepairRepositorySpareParts;
import cc.comac.mom.bean.EquipRepairRepositorySparePartsBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositoryDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositorySparePartsDataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
//@RequestMapping("/equip/repair/repository/SpareParts")
public class EquipRepairRepositorySparePartsServiceImpl implements EquipRepairRepositorySparePartsService{
	@Autowired
	private EquipRepairRepositorySparePartsDataHandler EquipRepairRepositorySparePartsDataHandler;
	@Autowired 
	private EquipRepairRepositoryDataHandler equipRepairRepositoryDataHandler;
	
//	@RequestMapping("/save")
	@Override
	public ResultBean save(@RequestBody EquipRepairRepositorySpareParts obj) {
		return EquipRepairRepositorySparePartsDataHandler.save(obj);
	}
	
//	@RequestMapping("/delete")
	@Override
	public ResultBean delete(@RequestBody EquipRepairRepositorySpareParts obj) {
		return EquipRepairRepositorySparePartsDataHandler.delete(obj);
	}
	
//	@RequestMapping("/update")
	@Override
	public ResultBean update(@RequestBody EquipRepairRepositorySpareParts obj) {
		return EquipRepairRepositorySparePartsDataHandler.update(obj);
	}
	
//	@RequestMapping("/saveBatch")
	@Override
	public ResultBean save(@RequestBody List<EquipRepairRepositorySpareParts> list) {
		ResultBean resultBean = new ResultBean(true);				
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("equip_category_id",  list.get(0).getEquipCategoryId());
		map.put("fault_cause_id", list.get(0).getFaultCauseId());
		map.put("row_status", 1);
		Collection<EquipRepairRepositoryEntity> listByMap = equipRepairRepositoryDataHandler.listByMap(map);
		if(listByMap != null && listByMap.size()>0) {
			//主表信息已存在
			long  equipRepairRepositoryId = 0;
			for(EquipRepairRepositoryEntity item:listByMap) {
				equipRepairRepositoryId = item.getId();				
			}
			for(EquipRepairRepositorySpareParts item:list) {
				item.getInnerData().setEquipRepairRepositoryId(equipRepairRepositoryId);
			}
			
		}else {
			//主表信息不存在
			EquipRepairRepository equipRepairRepository = new EquipRepairRepository();
			EquipRepairRepositoryEntity equipRepairRepositoryEntity = new EquipRepairRepositoryEntity();
			equipRepairRepositoryEntity.setEquipCategoryId(list.get(0).getEquipCategoryId());
			equipRepairRepositoryEntity.setFaultCauseId(list.get(0).getFaultCauseId());
			equipRepairRepository.setInnerData(equipRepairRepositoryEntity);
			ResultBean resultBean2 = equipRepairRepositoryDataHandler.save(equipRepairRepository);
			if(!resultBean2.success) {
				return resultBean2;
			}
			long  equipRepairRepositoryId = Long.valueOf(resultBean2.getMessage());
			for(EquipRepairRepositorySpareParts item:list) {
				item.getInnerData().setEquipRepairRepositoryId(equipRepairRepositoryId);
			}
		}
		ResultBean save = EquipRepairRepositorySparePartsDataHandler.save(list);
		if(!save.success) {
			return save;
		}
		return resultBean;
	}
	
//	@RequestMapping("/updateBatch")
	@Override
	public ResultBean update(@RequestBody List<EquipRepairRepositorySpareParts> list) {
		return EquipRepairRepositorySparePartsDataHandler.update(list);
	}
	
//	@RequestMapping("/deleteBatch")
	@Override
	public ResultBean delete(@RequestBody List<EquipRepairRepositorySpareParts> list) {
		return EquipRepairRepositorySparePartsDataHandler.delete(list);
	}
//	@RequestMapping("/getById/{id}")
	@Override
	public EquipRepairRepositorySpareParts getById(@PathVariable long id) {
		return EquipRepairRepositorySparePartsDataHandler.getById(id);
	}
	
//	@RequestMapping("/selectByCondition")
	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepairRepository obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = EquipRepairRepositorySparePartsDataHandler.selectByCondition(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}
	
//	@RequestMapping("/selectByFields")
	@Override
	public ResultBean selectByFields(@RequestBody EquipRepairRepositorySparePartsBean obj) {
		
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = EquipRepairRepositorySparePartsDataHandler.selectByFields(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}

}
